package com.lemon.file.service.impl;

import com.lemon.common.core.utils.StringUtils;
import com.lemon.file.domain.BiFileInfo;
import com.lemon.file.domain.vo.FolderVo;
import com.lemon.file.mapper.BiFileInfoMapper;
import com.lemon.file.service.ISysFileService;
import com.lemon.common.core.utils.file.FileUploadUtils;
import com.lemon.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 本地文件存储
 * 
 * @author huangpeng
 */
@Service
public class LocalSysFileServiceImpl implements ISysFileService
{
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 域名或本机访问地址
     */
    @Value("${file.domain}")
    public String domain;
    
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;

    @Autowired
    private BiFileInfoMapper biFileInfoMapper;

    /**
     * 本地文件上传接口
     * @Title: uploadFile
     * @Description: 本地文件上传接口
     * @param path 自定义的文件夹名，为空则随机生成
     * @param file 上传的文件
     * @param fileName 自定义的文件名，为空则随机生成
     * @return 访问地址,文件id，文件名
     * @throws:
     * @author: huangpeng
     * @Date: 2022/6/4 22:58
     */
    @Override
    public SysFile uploadFile(String path, MultipartFile file, String fileName) throws Exception
    {
        if (!StringUtils.isEmpty(path)) {
            // 上传到指定的文件夹
        }

        String name = FileUploadUtils.upload(localFilePath, file);
        String url = domain + localFilePrefix + name;
        SysFile sysFile = new SysFile();
        sysFile.setUrl(url);
        sysFile.setName(name);
        return sysFile;
    }

    @Override
    public void download(String fileName, String originFileName, HttpServletResponse response) throws Exception {

    }

    @Override
    public FolderVo listAll() {
        return null;
    }

    @Override
    public boolean deleteSourceFileByBatch(List<String> filenameList) throws Exception {
        return false;
    }

    @Override
    public List<BiFileInfo> selectBiFileInfoList(BiFileInfo biFileInfo) {
        return biFileInfoMapper.selectBiFileInfoList(biFileInfo);
    }

    @Override
    public int deleteBiFileInfoByIds(Long[] ids) {
        return 0;
    }

    @Override
    public List<BiFileInfo> selectFileInfoByIds(Long[] ids) {
        return null;
    }

    @Override
    public SysFile uploadFileWeb(String path, MultipartFile file, String fileName) throws Exception {
        return null;
    }

    @Override
    public List<BiFileInfo> listWed(BiFileInfo biFileInfo) {
        return null;
    }
}
